<?php
namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;
use Urameshibr\Requests\FormRequest;

class PassengerOrderRequest extends FormRequest
{


    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }


    /**
     * 默认验证规则
     * @return array
     */
    protected function getDefaultRules()
    {
        return [];
    }


    protected function failedValidation(Validator $validator)
    {
        throw new HttpResponseException(response()->json([
            'code' => 0,
            'msg'  => $validator->errors()->first()
        ]));
    }


    protected function getActionName()
    {
        return explode('@' , $this->route()[1]['uses'])[1];
    }


    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rule_action = 'getRulesBy'. ucfirst($this->getActionName());

        if(method_exists($this, $rule_action))
            return $this->$rule_action();

        return $this->getDefaultRules();
    }


    /**
     * 下单验证规则
     */
    public function getRulesByCreate()
    {
        return [
            'order_no' => ['bail', 'required'],
            'service_type_id' => ['bail', 'required']
        ];
    }


    public function attributes()
    {
        return [
            'order_no'=> '订单号',
            'service_type_id'=> '服务类型',
        ];
    }
}
